//
//  ThreeViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 DHF. All rights reserved.
//

#import "ThreeViewController.h"
#import "SevendayModel.h"
#import "AppTools.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface ThreeViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIPageControl *pageC;

@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.str = self.model.title;
    
    NSString *string = self.model.content168;
    
    self.str1 = self.model.imglink;
    
    self.str2 = self.model.imglink_2;
    
    self.str3 = self.model.imglink_3;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
    
    self.headLabel = [[UILabel alloc] init];
    self.headLabel.backgroundColor = [UIColor blackColor];
    self.num = 1;
    self.headLabel.text = @"1/3";
    self.headLabel.font = [UIFont boldSystemFontOfSize:16];
    self.headLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.headLabel];
    [self.headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(173);
        make.right.mas_equalTo(-138);
        
    }];
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
//    UIImageView *imageView1 = [[UIImageView alloc] init];
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 150*HEIGHT/667, WIDTH, 250*HEIGHT/667)];
    NSURL *url1 = [NSURL URLWithString:self.str1];
    [imageView1 sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    imageView1.userInteractionEnabled = YES;
    
    [imageView1 addGestureRecognizer:tap1];
    
    [self.scrollView addSubview:imageView1];
    
//    [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.left.mas_equalTo(0);
//        make.top.mas_equalTo(self.headLabel.mas_bottom).offset(80);
//       
//    }];
    
//    UIImageView *imageView2 = [[UIImageView alloc] init];
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH , 150*HEIGHT/667, WIDTH, 250*HEIGHT/667)];
    NSURL *url2 = [NSURL URLWithString:self.str2];
    [imageView2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    imageView2.userInteractionEnabled = YES;
    [imageView2 addGestureRecognizer:tap2];
    [self.scrollView addSubview:imageView2];
//    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.left.mas_equalTo(imageView1.mas_right).offset(0);
//        make.top.mas_equalTo(self.headLabel.mas_bottom).offset(80);
//       
//    }];
    
//    UIImageView *imageView3 = [[UIImageView alloc] init];
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*2 , 150*HEIGHT/667, WIDTH, 250*HEIGHT/667)];
    NSURL *url3 = [NSURL URLWithString:self.str3];
    [imageView3 sd_setImageWithURL:url3 placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    imageView3.userInteractionEnabled = YES;
    
    [imageView3 addGestureRecognizer:tap];
    
    [self.scrollView addSubview:imageView3];
    
//    [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.left.mas_equalTo(imageView2.mas_right).offset(0);
//        make.top.mas_equalTo(self.headLabel.mas_bottom).offset(80);
//        make.right.mas_equalTo(0);
//        
//    }];
    
    
//    self.footLabel = [[UILabel alloc] init];
    self.footLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH/375, 450*HEIGHT/667, 355*WIDTH/375, 100*HEIGHT/667)];
    self.footLabel.backgroundColor = [UIColor clearColor];
    self.footLabel.textColor = [UIColor whiteColor];
    self.footLabel.text = string;
    self.footLabel.font = [UIFont boldSystemFontOfSize:14];
    self.footLabel.numberOfLines = 0;
    [self.view addSubview:self.footLabel];
//    [self.footLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.left.mas_equalTo(10);
//        make.right.mas_equalTo(-10);
//        make.top.mas_equalTo(imageView1.mas_bottom).offset(50);
//        self.footLabel.numberOfLines = 0;
//        
//    }];
    
    self.scrollView.pagingEnabled = YES;
    // 取消边界反弹
    self.scrollView.bounces = NO;
    // 关闭滑动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    // 设置水平滑动
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.delegate = self;
    
    self.pageC = [[UIPageControl alloc] init];
    self.pageC.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.pageC];
    [self.pageC mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(175 / 2);
//        make.top.mas_equalTo(self.footLabel.mas_bottom).offset(167);
        make.right.mas_equalTo(- 175 / 2);
        make.bottom.mas_equalTo(- 50);
      
    }];
    // 设置页码个数
    self.pageC.numberOfPages = 3;
    // 设置当前页码颜色
    self.pageC.currentPageIndicatorTintColor = [UIColor whiteColor];
    // 设置未选中页码颜色
    self.pageC.pageIndicatorTintColor = [UIColor grayColor];
    
    //设置当前选中页码
    self.pageC.currentPage = 0;
    
    // 核心方法
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];

}

- (void)pageAction:(UIPageControl *)pageC
{
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width * pageC.currentPage, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速停止时触发");
    // 页码跟随图片
    self.pageC.currentPage = scrollView.contentOffset.x / self.view.frame.size.width;
    
    self.headLabel.text = [NSString stringWithFormat:@"%.f/3", self.scrollView.contentOffset.x / self.view.frame.size.width + 1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
